from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from Untils.db import db


db_conn = db(
    host="39.96.200.22",  # 数据库地址
    user="root",       # 数据库用户名
    password="Zhang185050.",
    database="roncoo_mini_pay_demo"  # 数据库名
)

# 查询测试数据
sql = "SELECT kw, canshu1, canshu2 FROM cs"  # 替换为实际表名
test_data = db_conn.select(sql)
print(test_data)
db_conn.close()  # 关闭数据库连接
# 初始化浏览器
driver = webdriver.Edge()  # 需确保ChromeDriver已配置

for kw, canshu1, canshu2 in test_data:
    if kw == "打开浏览器":
        # 无额外操作，浏览器已初始化
        pass
    elif kw == "网址":
        driver.get(canshu1)
    elif kw == "账号":
        locator_value = canshu1.strip("() ").split(',')[1].strip("' ")
        driver.find_element(By.NAME, locator_value).send_keys(canshu2)
    elif kw == "密码":
        locator_str = canshu1.strip("() ").split(',')[1].strip("' ")  # 去除括号、空格和单引号
        driver.find_element(By.NAME, locator_str).send_keys(canshu2)
    elif kw == "点击按钮":
        # 解析canshu1：如"(By.CLASS_NAME,'sub btn')" → 提取"sub btn"
        locator_str = canshu1.strip("() ").split(',')[1].strip("' ")
        # 转换为CSS选择器（用.连接多个类）
        css_selector = "." + locator_str.replace(" ", ".")  # "sub btn" → ".sub.btn"
        driver.find_element(By.CSS_SELECTOR, css_selector).click()
    elif kw == "强制等待":
        time.sleep(int(canshu1))
    # elif kw == "验证文本":
    #     # 假设验证某个元素的文本，需根据实际场景调整
    #     element_text = driver.find_element(By.XXX, "xxx").text  # 替换为实际定位方式和值
    #     assert "预期文本" in element_text, "文本验证失败"

# 关闭浏览器
driver.quit()